﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../demo.css">
    <script type="text/javascript" src="../jquery.min.js"></script>
    <script type="text/javascript" src="../jquery.cookie.js"></script>
    <script type="text/javascript" src="../jquery.easyui.min.js"></script>
    	<script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>

    <script type="text/javascript" src="../extend.js"></script>
    <script>
		var searchArg = {};
        var dataListCache = null;
        var dataCache = null;
        var employees =getEmployee();
        var customers =getCustomer();
        var productions = getProduction();
        var units = getUnit();
        var unitGroups = getUnitGroup();
        var undelivery = [];
        var appId = GetQueryString("moduleid");
        var rowEditing = -1;
        var editing = false;
        window.onresize = calSize;
        $(document).ready(function () {
			initBase();
            setData();
            calSize();
        });
        function initBase() {
            $('#CourierId').combobox({ data: employees });
            undelivery=dataFetcher("../../api/order/listundelivery");
        }
		function pageChanging(pageNumber, pageSize) {
            showMask("查找中");
            $.get('../../api/delivery/list', { Args: searchArg, Length: pageSize, Pos: (pageNumber-1) * pageSize }, function (data) {
                if (data.Result == 0) {
                    dataListCache = JSON.parse(data.Data);
                    $("#deliveryList").datagrid("loadData", dataListCache);
                    $("#deliveryList").datagrid('getPager').pagination({ total: data.Count, pageNumber: pageNumber, onSelectPage: pageChanging });
                } else {
                    $.messager.alert('提示', data.Message, 'error');
                }
                hideMask();
            }, 'json');
        }
        function setData() {
            pageChanging(1,10);
        }
        window.onresize = calSize;
        function calSize() {
            var height=window.parent.document.getElementById("mainFrame").clientHeight;
            var width=window.parent.document.getElementById("mainFrame").clientWidth;
            window.parent.document.getElementById(appId).height = height-32;
            $("#deliveryList").datagrid('resize',{height:height-76,width:width-15});
            $("#toolbar").panel({width:width-15});
            $("#editToolbar").panel({width:width-15});
            $("#detailEditToolBar").panel({width:width-15});
            
            $("#editZone").panel('resize',{height:height-76,width:width-15});
            $("#editZone").panel('resize',{width:width-15,height:288});
            $("#deliveryDetailList").datagrid('resize',{width:width-15,height:height-402});
        }
        function initVal() {
            $('#approveIcon').hide();
            $('#AutoId').val(dataCache.AutoId);
            $('#Flag').val(dataCache.Flag);
            $('#Site').val(dataCache.Site);
            $('#Code').textbox("setValue", dataCache.Code);
            $('#Note').textbox("setValue", dataCache.Note);
            $('#ReceiveDate').textbox("setValue", dataCache.ReceiveDate);
            $('#CourierId').combobox("setValue", dataCache.CourierId);
            listDetail(dataCache.AutoId);
        }
        function createNewRecord() {
            dataCache = new Object();
            var profile = JSON.parse( $.cookie('profile'));
            dataCache.ReceiveDate = new Date().toLocaleDateString();
            initVal();
            $('#editContent').window('open');
        }
        function editRecord(rowIndex, rowData) {
            dataCache = rowData;
            initVal();
            $('#editContent').window('open');
        }
        function submitChange() {
            if (dataCache == null) {
                $.messager.alert("错误", "无法获取被编辑的对象", "error");
                return;
            }
            showMask();
            dataCache.Flag = $("#Flag").val();
            dataCache.Code = $('#Code').textbox("getValue");
            dataCache.CourierId = $('#CourierId').combobox("getValue");
            dataCache.ReceiveDate = $('#ReceiveDate').datebox("getValue");
            dataCache.Note = $('#Note').textbox("getValue");
            //dataCache.DepartmentId = $('#DepartmentId').combobox("getValue");
            $.post("../../api/delivery/set", dataCache, function (response) {
                if (response.Result == 0) {
                    dataCache = JSON.parse(response.Data);
					submitDetail();
                    if(dataCache.Flag!=1){
                        for(var i=0;i<dataListCache.length;i++){
                            if(dataListCache[i].AutoId == dataCache.AutoId){
                                dataListCache.splice(i,1,dataCache);
								console.log('列表第'+i+'项被更新');
                                break;
                            }
                        }
                    }else{
						dataListCache.splice(0,0,dataCache);
						console.log('新项被追加到列表');
					}
                    $('#deliveryList').datagrid('loadData', dataListCache); 
                    $.messager.alert("信息","操作成功","info");
                    $('#editContent').window('close');
                }else{
                    $.messager.alert("错误",response.Message,"error");
					dataCache = oldData;
                }
            })
                .error(function () {
                    $.messager.alert("错误", "系统内部错误", "error");
                })
                .complete(function () {
                    hideMask();
                });
        }
        function boolFormatter(value, row, index) {
            if (value) {
                return "是";
            } else {
                return "否";
            }
        }
        function customerFormatter(value, row, index) {
            return valToName(customers, value);
        }
        function courierFormatter(value, row, index) {
            return valToName(employees, value);
        }

        function loadDataCache(rowIndex,rowData){
            dataCache = rowData;
			switch(dataCache.State){
				case 1:
					$('#btnDelete').linkbutton({disabled:false});
					$('#btnSubmit').linkbutton({disabled:false});
					$('#btnRecall').linkbutton({disabled:true});
					$('#btnApprove').linkbutton({disabled:true});
					$('#btnReject').linkbutton({disabled:true});
					$('#btnRevert').linkbutton({disabled:true});
					$('#approveIcon').hide();
					break;
				case 2:
					$('#btnDelete').linkbutton({disabled:true});
					$('#btnSubmit').linkbutton({disabled:true});
					$('#btnRecall').linkbutton({disabled:false});
					$('#btnApprove').linkbutton({disabled:false});
					$('#btnReject').linkbutton({disabled:false});
					$('#btnRevert').linkbutton({disabled:true});
					$('#approveIcon').hide();
					break;
				case 3:
					$('#btnDelete').linkbutton({disabled:true});
					$('#btnSubmit').linkbutton({disabled:true});
					$('#btnRecall').linkbutton({disabled:true});
					$('#btnApprove').linkbutton({disabled:true});
					$('#btnReject').linkbutton({disabled:true});
					$('#btnRevert').linkbutton({disabled:false});
					$('#approveIcon').show();
					break;					
				case 4:
					$('#btnDelete').linkbutton({disabled:true});
					$('#btnSubmit').linkbutton({disabled:true});
					$('#btnRecall').linkbutton({disabled:true});
					$('#btnApprove').linkbutton({disabled:true});
					$('#btnReject').linkbutton({disabled:true});
					$('#btnRevert').linkbutton({disabled:false});
					$('#approveIcon').show();
					break;
			}
        }
        function addItem() {
			if(editing){
				$('#deliveryDetailList').datagrid('endEdit',rowEditing);
				$('#deliveryDetailList').datagrid('insertRow',{index:rowEditing+1,row:{AutoId:0,Flag:0}});
            }else{
                $.messager.alert("消息","先按编辑键进入编辑状态","info");
            }
        }
		function removeItem(){
			if(rowEditing!=-1){
				$('#deliveryDetailList').datagrid('cancelEdit', rowEditing).datagrid('deleteRow',rowEditing);
			}else{
				$.messager.alert('提示','需要整行选中才能删除');
			}
        }
        function editTable() {
            if (!editing) {
                $('#deliveryDetailList').datagrid("enableCellEditing");
                editing = true;
                $('#detailAdd').linkbutton("enable");
                $('#detailDel').linkbutton("enable");
                $('#detailSave').linkbutton("enable");
                $('#detailCancel').linkbutton("enable");
                $('#detailEdit').linkbutton("disable");

            } else {
                $.messager.alert("消息", "当前正在编辑中", "info");
            }
        }
        function setRowIndex(index, row) {
            rowEditing = index;
        }
        function endEditTable() {
            $('#deliveryDetailList').datagrid('endEdit', rowEditing);
            $('#deliveryDetailList').datagrid("disableCellEditing");
            rowEditing = -1;
            editing = false;
            $('#detailAdd').linkbutton("disable");
            $('#detailDel').linkbutton("disable");
            $('#detailSave').linkbutton("disable");
            $('#detailCancel').linkbutton("disable");
            $('#detailEdit').linkbutton("enable");
        }
        function resetTable() {
			listDetail(dataCache.AutoId);
            endEditTable();
        }
        function listDetail(deliveryId) {
			
			if(deliveryId==undefined||deliveryId==0){
                $('#deliveryDetailList').datagrid("loadData",[]);
                return ;
            }
			$('#deliveryDetailList').datagrid("loading");
            $.get("../../api/delivery/listdetails?delivery=" + deliveryId, function (data) {
                if (data.Result == 0) {
                    var detail = JSON.parse(data.Data);
                    $('#deliveryDetailList').datagrid("loadData", detail);
                }
            }, "json")
                .error(function () {
                    $.messager.alert("错误", "系统内部错误", "error");
                })
                .complete(function () {
					$('#deliveryDetailList').datagrid("loaded");
                });
        }
        function submitDetail() {
            if (rowEditing != -1) {
                var valid = $('#deliveryDetailList').datagrid('validateRow', rowEditing);
                if (!valid) {
                    $.messager.alert("错误", "错误的数据格式", "error");
                    return;
                }
                $('#deliveryDetailList').datagrid('endEdit', rowEditing);
                var data = $('#deliveryDetailList').datagrid('getData');
                $.post("../../api/delivery/setdetails", { MasterId: dataCache.AutoId, Data: data.rows }, function (response) {
                    if (response.Result == 0) {
                        dataCache.Modules = JSON.parse(response.Data);
                    } else {
                        $.messager.alert("错误", response.Message, "error");
                    }
                })
                    .error(function () {
                        $.messager.alert("错误", "系统内部错误", "error");
                    })
                    .complete(function () {
                        endEditTable();
                    });
            } else {
                endEditTable();
            }
        }
        function productionFormatter(value, row, index) {
            return valToName(productions, value);
        }
        function unitFormatter(value, row, index) {
            return valToName(units, value);
        }  
        function unitGroupFormatter(value, row, index) {
            return valToName(unitGroups, value);
        }                
        function orderDetailSelected(n,o){
            for(var i=0;i<undelivery.length;i++){
                if(undelivery[i].AutoId==n){
                    var row =$('#deliveryDetailList').datagrid('getSelected');
                    row.OrderId=undelivery[i].OrderId;
                    row.ProductionId=undelivery[i].ProductionId;
                    row.UnitId=undelivery[i].UnitId;
                    row.SalesUnitId=undelivery[i].SalesUnitId;
                    row.CustomerId=undelivery[i].CustomerId;
                    row.UnitGroup=undelivery[i].UnitGroupId;
                    row.Quantity1=undelivery[i].Quantity1;
                    row.Quantity2=undelivery[i].Quantity2;
                }
            }
        }
        function orderCodeFormatter(value, row, index){
            for(var i=0;i<undelivery.length;i++){
                if(value==undelivery[i].OrderId){
                        return undelivery[i].OrderCode;
                }
            }
        }
        function orderDetailCodeFormatter(value, row, index){
            for(var i=0;i<undelivery.length;i++){
                if(value==undelivery[i].AutoId){
                        return undelivery[i].OrderCode;
                }
            }
        }
        function deleteRecord(){
			if (dataCache == null||dataCache.AutoId==0) {
                $.messager.alert("错误", "请先选中要删除的对象", "error");
                return;
            }
			$.messager.confirm('删除提示', '确定删除该记录?', function(r){
				if (r){
					showMask();
					$.get("../../api/delivery/removewithdetail?id="+dataCache.AutoId,function(response){
						if(response.Result==0){
							for(var i=0;i<dataListCache.length;i++){
								if(dataListCache[i].AutoId == dataCache.AutoId){
									dataListCache.splice(i,1);
									break;
								}
							}					
							dataCache = null;
							$('#deliveryList').datagrid('loadData', dataListCache); 
							$.messager.alert("信息","操作成功","info");
						}else{
							$.messager.alert("错误",response.Message,"error");
						}
					})
					.error(function(){
						$.messager.alert("错误","系统内部错误","error");
					})
					.complete(function() { 
						hideMask();
					});	
				}
			});		
		}
		function submitRecord(){
			showMask();
			$.get("../../api/delivery/submit?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					for(var i=0;i<dataListCache.length;i++){
						if(dataListCache[i].AutoId == dataCache.AutoId){
							dataListCache[i].State = 2;
							break;
						}
					}					
					dataCache = null;
					$('#deliveryList').datagrid('loadData', dataListCache); 
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
		}
		function recallRecord(){
			showMask();
			$.get("../../api/delivery/recall?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					for(var i=0;i<dataListCache.length;i++){
						if(dataListCache[i].AutoId == dataCache.AutoId){
							dataListCache[i].State = 1;
							break;
						}
					}					
					dataCache = null;
					$('#deliveryList').datagrid('loadData', dataListCache); 
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
			
		}
		function approveRecord(){
			showMask();
			$.get("../../api/delivery/approve?id="+dataCache.AutoId,function(response){
				if(response.delivery==0){
					for(var i=0;i<dataListCache.length;i++){
						if(dataListCache[i].AutoId == dataCache.AutoId){
							dataListCache[i].State = 3;
							break;
						}
					}					
					dataCache = null;
					$('#deliveryList').datagrid('loadData', dataListCache); 
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
			
		}
		function revertRecord(){
			showMask();
			$.get("../../api/delivery/revert?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					for(var i=0;i<dataListCache.length;i++){
						if(dataListCache[i].AutoId == dataCache.AutoId){
							dataListCache[i].State = 1;
							break;
						}
					}					
					dataCache = null;
					$('#deliveryList').datagrid('loadData', dataListCache); 
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
			
		}
		function rejectRecord(){
			showMask();
			$.get("../../api/delivery/reject?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					for(var i=0;i<dataListCache.length;i++){
						if(dataListCache[i].AutoId == dataCache.AutoId){
							dataListCache[i].State = 1;
							break;
						}
					}					
					dataCache = null;
					$('#deliveryList').datagrid('loadData', dataListCache); 
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
		}
    </script>
    </head>
<body onload = "hideMask()">
    <script>showMask('正在载入数据');</script>
            <div id="toolbar" class="easyui-panel" style="padding:1px;">
                <a href="#" class="easyui-linkbutton" data-options="onClick:createNewRecord,iconCls:'icon-edit',plain:true">新建</a>
                <a href="#" id="btnDelete" class="easyui-linkbutton" data-options="onClick:deleteRecord, iconCls:'icon-remove',plain:true">删除</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
                <a href="#" id="btnSubmit" class="easyui-linkbutton" data-options="onClick:submitRecord,iconCls:'icon-redo',plain:true,disabled:true">提审</a>
                <a href="#" id="btnRecall" class="easyui-linkbutton" data-options="onClick:recallRecord,iconCls:'icon-undo',plain:true,disabled:true">撤回</a>
                <a href="#" id="btnApprove" class="easyui-linkbutton" data-options="onClick:approveRecord,iconCls:'icon-ok',plain:true,disabled:true">审核</a>
                <a href="#" id="btnRevert" class="easyui-linkbutton" data-options="onClick:revertRecord,iconCls:'icon-cancel',plain:true,disabled:true">反审</a>
                <a href="#" id="btnReject" class="easyui-linkbutton" data-options="onClick:rejectRecord,iconCls:'icon-clear',plain:true,disabled:true">退回</a>
            </div>
            <div style="margin:2px"></div>
            <table id="deliveryList" class="easyui-datagrid"
                data-options="pagination:true,rownumbers: true,singleSelect:true,method:'get',onClickRow:loadDataCache,onDblClickRow:editRecord,striped:true,">
                <thead>
                    <tr>
                        <th data-options="field:'AutoId',hidden:true"></th>
                        <th data-options="field:'Flag',hidden:true"></th>
                        <th data-options="field:'Code',width:120,align:'right'">物流单号</th>
                        <th data-options="field:'CourierId',width:120,align:'right',formatter:courierFormatter">物流人员</th>
                        <th data-options="field:'ReceiveDate',width:120,align:'right'">销售日期</th>
                        <th data-options="field:'Note' ,width:120,align:'right'">备注</th>
                        <th data-options="field:'State',width:100,formatter:stateFormatter">状态</th>
                    </tr>
                </thead>
            </table>
    <div id="editContent" class="easyui-window" title="编辑框" data-options="noheader:true,maximized:true,modal:true,closed:true,iconCls:'icon-save',border:false" style="padding:5px 5px 0px 5px">
        <div class="easyui-panel" id="editToolbar" style="padding:1px;width:100%">
            <a href="#" class="easyui-linkbutton" data-options="onClick:submitChange,iconCls:'icon-save',plain:true">保存</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:initVal,iconCls:'icon-no',plain:true">取消</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:function(){$('#editContent').window('close');},iconCls:'icon-blank',plain:true">关闭</a>
        </div>
                <div style="margin:2px"></div>
                <div id="editZone" class="easyui-panel" style="padding:10px;">
                    <table>
                        <tr>
                            <td><input class="easyui-textbox" id="Code" style="width:300px" data-options="label:'单号:'"></td>
                            <td>&nbsp;</td>
                            <td><input class="easyui-combobox" id="CourierId" style="width:300px" data-options="label:'物流人员:',valueField: 'AutoId',textField: 'Name'"></td>
                        </tr>
                        <tr>
                            <td><input class="easyui-datebox" id="ReceiveDate" style="width:300px" data-options="label:'送货日期:',formatter:dateFormatter,parser:dateParser"></td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="3"><input class="easyui-textbox" id="Note" style="width:700px" data-options="label:'备注:'"></td>
                        </tr>
                    </table><div id='approveIcon' hidden=true style="z-index:999;position: absolute;top: 50px;right: 50px;width: 216px;height:157px;background: url(../image/approved.png)"></div>
                    <div hidden>
                        <p id="AutoId"></p>
                        <p id="Flag"></p>
                        <p id="Site"></p>
                        <p id="State"></p>
                    </div>
                </div>
                <div style="margin:2px"></div>
                    <div class="easyui-panel" id="detailEditToolBar" style="padding:1px;width:100%">
                    <a href="#" id="detailEdit" class="easyui-linkbutton" data-options="onClick:editTable,iconCls:'icon-edit',plain:true">开始</a>
                    <a href="#" id="detailAdd"  class="easyui-linkbutton" data-options="onClick:addItem,iconCls:'icon-add',plain:true,disabled:true">增行</a>
                    <a href="#" id="detailDel"  class="easyui-linkbutton" data-options="onClick:removeItem,iconCls:'icon-remove',plain:true,disabled:true">删行</a>
                    <a href="#" id="detailCancel"   class="easyui-linkbutton" data-options="onClick:resetTable,iconCls:'icon-no',plain:true,disabled:true">取消</a>
                </div>
                <div style="margin:2px"></div>
                <table id="deliveryDetailList" class="easyui-datagrid" style="width:100%;" data-options="singleSelect:true,method:'get',onClickRow:setRowIndex">
                    <thead>
                        <tr>
                            <th data-options="field:'AutoId',hidden:true"></th>
                            <th data-options="field:'Flag',hidden:true"></th>
                            <th data-options="field:'DeliveryId',hidden:true"></th>
                            <th data-options="field:'OrderDetailId',width:120,align:'right',
								formatter:orderDetailCodeFormatter,
								editor:{
									type:'combogrid',
									options:getComboGridInfo(null,'OrderCode',[[
											{field:'AutoId',title:'ID',width:60},
											{field:'OrderCode',title:'代号',width:60},
											{field:'CustomerName',title:'客户',width:100},
											{field:'ProductionName',title:'产品',width:100}
										]],dataFetcher('../../api/order/listundelivery'),orderDetailSelected)
								}">订单</th>
                            <th data-options="field:'OrderId',width:100,align:'right',hidden:true">订单</th>
                            <th data-options="field:'CustomerId',width:100,align:'right',formatter:customerFormatter">客户</th>
                            <th data-options="field:'ProductionId',width:100,align:'right',formatter:productionFormatter">品名</th> 
                            <th data-options="field:'UnitGroup',width:100,align:'right',formatter:unitGroupFormatter">单位组</th> 
                            <th data-options="field:'UnitId',width:100,align:'right',formatter:unitFormatter">标准单位</th>           
                            <th data-options="field:'SalesUnitId',width:100,align:'right',formatter:unitFormatter">销售单位</th>
                            <th data-options="field:'Quantity1',width:100,align:'right'">销售数量</th>
                            <th data-options="field:'Quantity2',width:100,align:'right'">标准单位数量</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </body>
</html>